package lsl.leetCode.year2023.test0206;

/**
 * description:面试题 10.01. 合并排序的数组
 *
 * @author: liushengli
 * data: 2023/2/6
 */
public class test10 {
    public static void main(String[] args) {
        int[] A = {1, 2, 3, 0, 0, 0};
        int[] B = {2, 5, 6};
        int[] merge = merge(A, 3, B,3);
        System.out.println(merge);
    }

    public static int[] merge(int[] A, int m, int[] B, int n) {
        int[] arr = new int[m + n];
        int a = 0;
        int b = 0;
        int index = 0;
        while (a < m || b < n) {
            if (a < m && b < n) {
                if (A[a] <= B[b]) {
                    arr[index++] = A[a];
                    a++;
                } else {
                    arr[index++] = B[b];
                    b++;
                }
            } else {
                if (a < m) {
                    arr[index++] = A[a];
                    a++;
                }
                if (b < n) {
                    arr[index++] = B[b];
                    b++;
                }
            }
        }
        for (int i = 0; i < index; i++) {
            A[i] = arr[i];
        }
        return A;
    }
}
